package com.goodcol.plugin.ui;

import com.goodcol.plugin.entity.TypeMapper;
import com.goodcol.plugin.enums.MatchType;
import com.goodcol.plugin.tool.Constants;
import com.goodcol.plugin.tool.SettingGroupUtils;
import com.goodcol.plugin.tool.StringUtils;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.ComboBox;
import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.util.ui.JBUI;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * TypeMapper选择并添加对话框
 *
 * @author 单红宇
 * @date 2024/2/2 13:27
 */
public class TypeMapperAddDialog extends DialogWrapper {

    /**
     * 数据库字段类型，例如 varchar(20)、number、int 等
     */
    private String typeName;

    /**
     * mainPanel
     */
    private JPanel mainPanel;

    /**
     * comboBox
     */
    private ComboBox<String> comboBox;

    /**
     * 构造方法
     *
     * @param project  project
     * @param typeName 数据库字段类型，例如 varchar(20)、number、int 等
     */
    public TypeMapperAddDialog(@Nullable Project project, String typeName) {
        super(project);
        this.typeName = typeName;
        this.initPanel();
    }

    /**
     * initPanel
     */
    private void initPanel() {
        setTitle(Constants.TITLE_INFO);
        String msg = String.format("数据库类型%s，没有找到映射关系，请输入想转换的类型？", typeName);
        JLabel label = new JLabel(msg);
        this.mainPanel = new JPanel(new BorderLayout());
        this.mainPanel.setBorder(JBUI.Borders.empty(5, 10, 7, 10));
        mainPanel.add(label, BorderLayout.NORTH);
        this.comboBox = new ComboBox<>(Constants.DEFAULT_JAVA_TYPE_LIST);
        this.comboBox.setEditable(true);
        this.mainPanel.add(this.comboBox, BorderLayout.CENTER);
        init();
    }

    @Override
    protected @Nullable JComponent createCenterPanel() {
        return this.mainPanel;
    }

    @Override
    protected void doOKAction() {
        String selectedItem = (String) this.comboBox.getSelectedItem();
        if (StringUtils.isEmpty(selectedItem)) {
            super.doCancelAction();
            return;
        }
        super.doOKAction();
        typeName = typeName.toLowerCase();
        // matcher1 对应 abc(1)/abc(1) unsigned 这种格式
        Matcher matcher1 = Pattern.compile("(\\w+)\\(\\d+\\)(.*)").matcher(typeName);
        // matcher2 对应 abc(3,2)/abc(3,2) unsigned 这种格式
        Matcher matcher2 = Pattern.compile("(\\w+)\\(\\d+,\\d+\\)(.*)").matcher(typeName);
        MatchType matchType = MatchType.ORDINARY;
        if (matcher1.matches()) {
            matchType = MatchType.REGEX;
            typeName = matcher1.group(1).concat("(\\(\\d+\\)(.*))?");
        } else if (matcher2.matches()) {
            matchType = MatchType.REGEX;
            typeName = matcher2.group(1).concat("(\\(\\d+,\\d+\\)(.*))?");
        }
        TypeMapper typeMapper = new TypeMapper();
        typeMapper.setMatchType(matchType);
        typeMapper.setJavaType(selectedItem);
        typeMapper.setColumnType(typeName);
        // 缺失的类型，弹框配置后，默认添加到Default中
        SettingGroupUtils.getDefaultTypeMapperGroup().getElementList().add(typeMapper);
    }
}